<template>
  <div class="login">
    <h2>登录</h2>
    <div v-if="error" class="error-message">{{ error }}</div>
    <form @submit.prevent="handleLogin" class="login-form" :class="{ 'loading': isLoading }">
      <div class="form-item">
        <label>账号：</label>
        <input type="text" v-model="account" required />
      </div>
      <div class="form-item">
        <label>密码：</label>
        <input type="password" v-model="password" required />
      </div>
      <div class="form-item remember-me">
        <label class="checkbox-label">
          <input type="checkbox" v-model="rememberMe">
          记住登录状态（7天）
        </label>
      </div>
      <div class="form-actions">
        <button type="submit" class="btn-login" :disabled="isLoading">
          <loading-spinner v-if="isLoading" text="登录中..." />
          <span v-else>登录</span>
        </button>
        <router-link to="/register" class="btn-register" :class="{ 'disabled': isLoading }">注册账号</router-link>
      </div>
    </form>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { login } from '../api/user'
import LoadingSpinner from '../components/LoadingSpinner.vue'

const router = useRouter()
const store = useStore()
const account = ref('')
const password = ref('')
const error = ref('')
const isLoading = ref(false)
const rememberMe = ref(false)

const handleLogin = async () => {
  if (isLoading.value) return
  error.value = ''
  isLoading.value = true
  
  try {
    console.log('Login request:', { account: account.value, password: password.value })
    const response = await login({
      account: account.value,
      password: password.value
    })
    console.log('Login response:', response)
    
    if (response.code === 200) {
      // 存储用户信息
      store.commit('setUser', response.data)
      store.commit('setToken', response.data.account)
      
      // 记住登录
      if (rememberMe.value) {
        localStorage.setItem('rememberMe', 'true')
      }
      
      alert('登录成功')
      router.push('/')
    } else {
      error.value = response.msg || '登录失败'
    }
  } catch (err) {
    console.error('Login error:', err)
    error.value = '登录失败：' + err.message
  } finally {
    isLoading.value = false
  }
}
</script>

<style scoped>
.login {
  max-width: 400px;
  margin: 40px auto;
  padding: 20px;
  border: 1px solid #eee;
  border-radius: 8px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

input {
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.form-actions {
  display: flex;
  gap: 10px;
}

.btn-login, .btn-register {
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  text-decoration: none;
}

.btn-login {
  background-color: #1e88e5;
  color: white;
  border: none;
}

.btn-register {
  background-color: #f5f5f5;
  color: #333;
}

.error-message {
  color: #f44336;
  margin-bottom: 15px;
  padding: 10px;
  background-color: #ffebee;
  border-radius: 4px;
  font-size: 14px;
}

.loading {
  opacity: 0.7;
  pointer-events: none;
}

.disabled {
  opacity: 0.5;
  pointer-events: none;
}

.remember-me {
  flex-direction: row !important;
  align-items: center;
  gap: 8px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  user-select: none;
}

input[type="checkbox"] {
  width: 16px;
  height: 16px;
  cursor: pointer;
}
</style> 